var app = getApp();

Page({
  data: {
    name:'',
    tel:'',
    address_xq:'',
    order_id:''
  },
  nameCheck:function(e){
    var names = e.detail.value
    if (names.length === 0) {
      wx.showToast({
        title: '请填写姓名',
        icon: 'none'
      })
    } else if (names.length>10){
      wx.showToast({
        title: '姓名不得超过10个字符',
        icon: 'none'
      })
    }
  },
  phoneCheck:function(e){
    var telphone = e.detail.value
    if (telphone.length === 0) {
      wx.showToast({
        title: '请填写手机号',
        icon: 'none'
      })
    }
    else if (!(/^1(3|4|5|7|8)\d{9}$/.test(telphone))) {
      wx.showToast({
        title: '手机号格式不正确',
        icon:'none'
      })
    }
  },
  address:function(e){
    var addresses = e.detail.value
    if (addresses.length === 0) {
      wx.showToast({
        title: '请填写地址',
        icon: 'none'
      })
    }
  },
  formSubmit: function (e) {
    var  that=this
    var adds = e.detail.value;
    console.log(adds);
    //对手机号进行审核
    if (!(/^1(3|4|5|7|8)\d{9}$/.test(adds.phone))) {
        wx.showToast({
              title: '请填写正确的手机号',
              duration: 2000
            });
    
    } else {
      var cartId = this.data.cartId;
      wx.request({
        url: app.d.ceshiUrl + '/Api/Order/order_add_mod',
        data: {
          order_id: that.data.order_id,
          address_xq: adds.address_xq,
          receiver: adds.name,
          tel: adds.phone,
          uid: app.d.userId,
        },
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {// 设置请求的 header
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          // success
          var status = res.data.status;
          if (status == 1) {
            wx.showToast({
              title: '修改成功！',
              duration: 2000
            });
            setTimeout(function () {
              wx.redirectTo({
                   url: (app.globalData.froms=='company_user' ? '../company_user/dingdan?currentTab=1&otype=deliver' : '../user/dingdan?currentTab=1&otype=deliver') ,
              });
            }
              , 1000)
          } else {
   
              wx.showToast({
                title: res.data.err,
                duration: 2000,
                icon:'none'
              });
          }
        },
        fail: function () {
          wx.showToast({
            title: '网络不给力！',
            duration: 2000
          });
        }
      })
      this.setData({
        tel:adds.phone
      })
    }
  },

  onLoad: function (options) {
    console.log(app.globalData.froms);
    var that = this;
    if (app.globalData.froms == 'user') {
      wx.setNavigationBarColor({
        frontColor: 'white',
        backgroundColor: '#e93d3b',
      })
    } else {
      wx.setNavigationBarColor({
        frontColor: 'white',
        backgroundColor: '#e93d3b',
      })
    }
    console.log(options);
      that.setData({
         order_id:options.orderId,
      })
    wx.request({
        url: app.d.ceshiUrl + '/Api/Order/order_details',
        data: {
          order_id: options.orderId,
        },
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {// 设置请求的 header
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          // success
          var status = res.data.status;
           var ord = res.data.ord;
          if (status == 1) {
                 that.setData({
                    name:ord.receiver,
                    tel:ord.tel,
                    address_xq:ord.address_xq,
                   })
          } else {
   
              wx.showToast({
                title: res.data.err,
                duration: 2000,
                icon:'none'
              });
          }
        },
        fail: function () {
          wx.showToast({
            title: '网络不给力！',
            duration: 2000
          });
        }
      })
  },
  onShareAppMessage: function () {
    return {
      title: '',
      path: '',
      success: function (res) {
        // 分享成功
      },
      fail: function (res) {
        // 分享失败
      }
    }
  },

})
